<?php
namespace App\Http\Controllers\Api\V2\Today;

use App\Http\Controllers\ApiController;
use App\Http\Controllers\Business\App\Today\Today;
use App\Http\Controllers\Business\App\MyCenter\MyCenter;
use App\Http\Controllers\Business\App\Auth\AppAuth;
use App\Http\Controllers\Business\App\PublicBusiness;
use App\Http\Controllers\Business\App\Statistic\StatisticManagement;

class TodayController extends ApiController
{
    //首页中的 banner
    public function indexBanner() {
        if(!$this->request->has('cityid')) {
            return $this->responseError('无效参数');
        }
        $cityId = $this->request->get('cityid');
        $bannerResult = Today::index($cityId);
        if($bannerResult['status'] == 'failed') {
            return $this->responseError($bannerResult['message']);
        }
        $data = $bannerResult['data'];
        if($this->request->has('mylongitude')) {
            $myLongitude = $this->request->get('mylongitude');
        } else {
            $myLongitude = '';
        }
        if($this->request->has('mylatitude')) {
            $myLatitude = $this->request->get('mylatitude');
        } else {
            $myLatitude = '';
        }
        $event = 'today';
        $shopResult = PublicBusiness::shopList($cityId,$event,$myLongitude,$myLatitude);
        if($shopResult['status'] == 'failed') {
            return $this->responseError($shopResult['message']);
        }
        $data['shop'] = $shopResult['data'];
        $data['today'] = date('Y-m-d');
        if($this->request->has('iOSorAndroid')) {
            $userAgent = $this->request->get('iOSorAndroid');
        } else {
            $userAgent = $this->request->header('user-agent');
        }
        $ip = $this->request->ip();
        StatisticManagement::addUserLoginAfterInfo('','index',$userAgent,$ip);
        return $this->responseSuccessData($data);
    }
    //首页中的商家部分
    public function indexShop() {
        if(!$this->request->has('cityid')) {
            return $this->responseError('无效参数');
        }
        $cityId = $this->request->get('cityid');
        $event = 'today';  //today首页 next预告
        if($this->request->has('mylongitude')) {
            $myLongitude = $this->request->get('mylongitude');
        } else {
            $myLongitude = '';
        }
        if($this->request->has('mylatitude')) {
            $myLatitude = $this->request->get('mylatitude');
        } else {
            $myLatitude = '';
        }
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $shopResult = PublicBusiness::shopList($cityId,$event,$myLongitude,$myLatitude);
        if($shopResult['status'] == 'failed') {
            return $this->responseError($shopResult['message']);
        }
        $data = $shopResult['data'];

        $message = '获取信息成功';
        return $this->responseSuccess($message, $data);
    }
    //商家列表
    public function shopLists() {
        $cityId = $this->request->get('cityid');
        if($this->request->has('event')) {
            $event = $this->request->get('event'); //event的值有 today(今日),next(预告),occur(晚九点)
        } else {
            $event = 'today';
        }
        if($this->request->has('mylongitude')) {
            $myLongitude = $this->request->get('mylongitude');
        } else {
            $myLongitude = '';
        }
        if($this->request->has('mylatitude')) {
            $myLatitude = $this->request->get('mylatitude');
        } else {
            $myLatitude = '';
        }
        $parameter = [];
        if($this->request->has('firstclassid')) {
            $parameter['firstclassid'] = $this->request->get('firstclassid');
        }
        if($this->request->has('secondclassid')) {
            $parameter['secondclassid'] = $this->request->get('secondclassid');
        }
        if($this->request->has('districtid')) {
            $parameter['districtid'] = $this->request->get('districtid');
        }
        if($this->request->has('businessareaid')) {
            $parameter['businessareaid'] = $this->request->get('businessareaid');
        }
        if($this->request->has('nextevent')) {
            $parameter['nextevent'] = $this->request->get('nextevent');
        }
        if($this->request->has('sort')) {
            $sort = $this->request->get('sort');
        } else {
            $sort = '';
        }
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $result = PublicBusiness::shopList($cityId, $event, $myLongitude, $myLatitude, $parameter, $sort);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $message = '获取信息成功';
        $data = $result['data'];
        return $this->responseSuccess($message, $data);
    }
    //通过商家id获取商家详情
    public function shopDetail() {
        $shopId = $this->request->get('shopid');
        $result = PublicBusiness::shopDetails($shopId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $message = '获取信息成功';
        $data = $result['data'];
        //获取
        $token = $this->request->get('token');
        $userId = AppAuth::tokenGetUserId($token);
        $collectionStatus = '0';
        $subscriptionStatus = '0';
        if($userId) {
            $collectionResult = MyCenter::getCollectionStatus($userId, $shopId);
            $subscriptionResult = MyCenter::getSubscriptionStatus($userId, $shopId);
            if($collectionResult['status'] == 'success') {
                $collectionStatus = '1';
            }
            if($subscriptionResult['status'] == 'success') {
                $subscriptionStatus = '1';
            }
        }
        $data['collectionstatus'] = $collectionStatus;
        $data['subscriptionstatus'] = $subscriptionStatus;
        return $this->responseSuccess($message, $data);
    }
    //通过商家id获取商家活动日
    public function getShopEvent() {
        $shopId = $this->request->get('shopid');
        $result = PublicBusiness::getTwoMonthDoubleCalendar($shopId);
        $data = [];
        $lists = $result['data'];
        foreach($lists as $list) {
            $event = $list['event'];
            array_push($data, date('Y-m-d', strtotime($event)));
        }
        return $this->responseSuccessData($data);
    }
    //搜索商家
    public function searchShop() {
        if($this->request->has('cityid')) {
            $cityId = $this->request->get('cityid');
        } else {
            $cityId = '1';
        }
        $search = $this->request->get('search');
        if($this->request->has('mylontitude')) {
            $myLongitude = $this->request->get('mylongtitude');
        } else {
            $myLongitude = '';
        }
        if($this->request->has('mylatitude')) {
            $myLatitude = $this->request->get('mylatitude');
        } else {
            $myLatitude = '';
        }
        $result = PublicBusiness::searchShop($cityId, $search, $myLongitude,$myLatitude);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $message = '获取信息成功';
        $data = $result['data'];
        return $this->responseSuccess($message, $data);
    }
    //热门搜索词
    public function getSearchWords() {
        if($this->request->has('cityid')) {
            $cityId = $this->request->get('cityid');
        } else {
            $cityId = 1; //默认南昌
        }
        $result = PublicBusiness::getHotWordList($cityId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $lists = $result['data'];
        $data = [];
        foreach($lists as $list) {
            array_push($data, $list['name']);
        }
        //$data = ["美发","KTV","串串","火锅","花店","酒店","烤全羊","万达"];
        return $this->responseSuccessData($data);
    }
    //通过商家id获取商家评论列表, type可以为空， type=1表示为仅指带图的评论
    public function shopCommentList() {
        $shopId = $this->request->get('shopid');
        $type = $this->request->get('type');
        $result = PublicBusiness::commentList($shopId, $type);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $message = '获取信息成功';
        $data = $result['data'];
        if(isset($data['picture'])) {
            $picture = $data['picture'];
            $data['picture'] = json_decode($picture, true);
        }
        return $this->responseSuccess($message, $data);
    }
    //增加评论
    public function addComment() {
        $token = $this->request->get('token');
        $userId = AppAuth::tokenGetUserId($token);
        $orderNumber = $this->request->get('ordernumber');
        if((!$this->request->has('score'))|| (!$this->request->has('content'))) {
            return $this->responseError('评论或者评分不能为空');
        }
        $score = $this->request->get('score');
        $content = $this->request->get('content'); //TODO 字数限制
        $picture = $this->request->get('picture');
        $result = PublicBusiness::newComment($orderNumber, $userId, $score, $content, $picture);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $data = [];
        return $this->responseSuccess($result['message'], $data);
    }

}